package cn.ryh.gat1400.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: aiLin
 * @Date: 2020/9/28 16:35
 * @desc:
 */
@Slf4j
public class BenUtils {

        /**

         @Title: beanTobeanLowerCase
         @Description: 对象bean间属性值复制：无视大小写和下划线_和横杠-
         @param object 已赋值的bean
         @param cls 待赋值的bean的class
         @return 返回复制后的目标bean
         @throws Exception
         @author author
         @date 2020-03-31 03:40:56
         */
        public static Object beanTobeanLowerCase(Object object, Class<?> cls) throws Exception {
            Object obj = cls.newInstance();
            if (object != null) {
                Class<?> clsOld = object.getClass();
                Field[] fieldsOld = clsOld.getDeclaredFields();
                Field[] fields = obj.getClass().getDeclaredFields();
                for (Field fieldOld : fieldsOld) {
                    String fieldNameOld = fieldOld.getName().replace("-","");
                    fieldNameOld = fieldNameOld.replace("","");
                    int modOld = fieldOld.getModifiers();
                    if (Modifier.isPrivate(modOld) && !Modifier.isFinal(modOld)) {
                        fieldOld.setAccessible(true);
                        for (Field field : fields) {
                            String fieldName = field.getName().replace("-","");
                            fieldName = field.getName().replace("","");
                            int mod = field.getModifiers();
                            if (Modifier.isPrivate(mod) && !Modifier.isFinal(mod)) {
                                field.setAccessible(true);
                                if (fieldNameOld.equalsIgnoreCase(fieldName)) {
                                    String result = String.valueOf(fieldOld.get(object));
                                    if(StringUtils.isEmpty(result) || "null".equals(result)){
                                        continue;
                                    }
                                    field.set(obj, String.valueOf(fieldOld.get(object)));
                                }
                            }
                        }
                    }
                }
            }
            return obj;
        }

    /**

     @Title: beanTobeanLowerCase
     @Description: 对象bean间属性值复制：无视大小写和下划线_和横杠-
     @param object 已赋值的bean
     @param cls 待赋值的bean的class
     @return 返回复制后的目标bean
     @throws Exception
     @author author
     @date 2020-03-31 03:40:56
     */
    public static Object beanTobeanLowerCase2(Object object, Class<?> cls) throws Exception {
        Object obj = cls.newInstance();
        if (object != null) {
            Class<?> clsOld = object.getClass();
            Field[] fieldsOld = clsOld.getDeclaredFields();
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field fieldOld : fieldsOld) {
                String fieldNameOld = fieldOld.getName().replace("-","");
                fieldNameOld = fieldNameOld.replace("","");
                int modOld = fieldOld.getModifiers();
                if (Modifier.isPrivate(modOld) && !Modifier.isFinal(modOld)) {
                    fieldOld.setAccessible(true);
                    for (Field field : fields) {
                        String fieldName = field.getName().replace("-","");
                        fieldName = field.getName().replace("","");
                        int mod = field.getModifiers();
                        if (Modifier.isPrivate(mod) && !Modifier.isFinal(mod)) {
                            field.setAccessible(true);
                            if (fieldNameOld.equalsIgnoreCase(fieldName)) {
                                String filedsInfoType = getFiledsInfoType(object,fieldNameOld);
                                //log.info("原对象字段：{}，类型：{}",fieldNameOld,filedsInfoType);
                                String fieldNameType = getFiledsInfoType(obj,fieldName);
                                //log.info("转换后字段：{}，类型：{}",fieldName,fieldNameType);
                                if(fieldNameType.equals(filedsInfoType)){
                                    field.set(obj,fieldOld.get(object));
                                }else if(!fieldNameType.equals(filedsInfoType)){
                                    if(fieldNameType.contains("String")){
                                        field.set(obj, String.valueOf(fieldOld.get(object)));
                                    }else if(fieldNameType.contains("Integer") || fieldNameType.contains("int")){
                                        field.set(obj, Integer.parseInt(String.valueOf(fieldOld.get(object))));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return obj;
    }


    /**
     * 根据属性名获取属性值
     * */
    private Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return null;
        }
    }

    /**
     * 获取属性名数组
     * */
    private String[] getFiledName(Object o){
        Field[] fields=o.getClass().getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        for(int i=0;i<fields.length;i++){
            System.out.println("类型"+fields[i].getType());
            System.out.println("名称="+fields[i].getName());
            fieldNames[i]=fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 获取属性类型(type)，属性名(name)，属性值(value)的map组成的list
     * */
    private List getFiledsInfo(Object o){
        Field[] fields=o.getClass().getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        List list = new ArrayList();
        Map infoMap=null;
        for(int i=0;i<fields.length;i++){
            infoMap = new HashMap();
            infoMap.put("type", fields[i].getType().toString());
            infoMap.put("name", fields[i].getName());
            infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
            list.add(infoMap);
        }
        return list;
    }

    /**
     * 获取属性类型(type)
     * */
    private static String getFiledsInfoType(Object o,String fName){
        Field[] fields=o.getClass().getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        for(int i=0;i<fields.length;i++){
            String name = fields[i].getName();
            if(name.equals(fName)){
                return fields[i].getType().toString();
            }
        }
        return "";
    }

    /**
     * 获取对象的所有属性值，返回一个对象数组
     * */
    public Object[] getFiledValues(Object o){
        String[] fieldNames=this.getFiledName(o);
        Object[] value=new Object[fieldNames.length];
        for(int i=0;i<fieldNames.length;i++){
            value[i]=this.getFieldValueByName(fieldNames[i], o);
        }
        return value;
    }
}


